package cn.gdcp.service;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private Button btnStart;
    private Button btnStop;
    private Button btnBind;
    private Button btnUnBind;
    private ServiceConnection serviceConnection;
    private MyBinder binder;
    private Button btnadd;
    private Button btnTime;
    private TextView tvTime;
    private EditText edtHuiwen;
    private Button btnHuiwen;
    private TextView txtHuiwen;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                Log.e("MainActivity","onServiceConnected");
                binder = (MyBinder) iBinder;
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                Log.e("MainActivity","onServiceConnected");
            }
        };


        btnStart = findViewById(R.id.start_service);
        btnStop = findViewById(R.id.stop_service);
        btnBind = findViewById(R.id.bind_service);
        btnUnBind = findViewById(R.id.unbind_service);
        btnadd = findViewById(R.id.btn_add);
        btnTime = findViewById(R.id.btn_time);
        tvTime = findViewById(R.id.tv_time);
        edtHuiwen = findViewById(R.id.edt_txt);
        btnHuiwen = findViewById(R.id.btn_huiwen);
        txtHuiwen = findViewById(R.id.tv_huiwen);

        btnBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                bindService(intent,serviceConnection,BIND_AUTO_CREATE);
            }
        });


        btnadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int result= binder.add(100,200);
                Log.e("binder.add","result = " + result);

                String str = binder.toUpperCase("Hello World");
                Log.e("binder.toUpperCase","str = " + str);

                String str1 = binder.toLowerCase("BBS");
                Log.e("binder.toLowerCase","result = " + str1);

            }
        });

        btnUnBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("MyActivity","btnUnBind");
                unbindService(serviceConnection);
            }
        });

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                startService(intent);
            }
        });

        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                stopService(intent);
            }
        });

        btnHuiwen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = edtHuiwen.getText().toString();
                String str = binder.reverseWord(s);
                txtHuiwen.setText(str);
                Log.e("ReverseWord","str" + str);
            }
        });

        btnTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String time = binder.getTime();
                tvTime.setText(time);
            }
        });
    }

}
